import { ITheme, Favico, Tag, Seting } from '@/typeing/dexie'
import Dexie from 'dexie';

export class MyAppDatabase extends Dexie {
  themes!: Dexie.Table<ITheme, number>;
  favico!: Dexie.Table<Favico, number>;
  historyTag!: Dexie.Table<Tag, number>;
  seting!: Dexie.Table<Seting, number>;
  // phones!: Dexie.Table<IPhoneNumber, number>;
  
  constructor() {  
    super("MyAppDatabase")
    this.version(1).stores({
      themes: '++id',
      favico: '++id',
      historyTag: 'id',
      seting: 'id'
      // phones: '++id, contactId, type, phone',
    });
  }
}
const db = new MyAppDatabase()

// 主题
export class dexieTheme {
  id?: number

  constructor(id?: number) {
    if(id) this.id = id
  }
  
  async updateById(id: number, newObj:ITheme ){
    return db.themes.update(id, newObj)
  }

  async selectAll(){
    return await db.themes.filter(() => true).toArray()
  }

  async selectById(){
    return await db.themes.filter(() => true).toArray()
  }

  save(theme: ITheme) {
    return db.transaction('rw', db.themes, () => {
      return db.themes.put(theme)
    });
  }
}

// favico图标
export class dexieFavico {
  save(favico: Favico) {
    return db.transaction('rw', db.favico, () => {
      return db.favico.put(favico)
    });
  }
}

// 记录书签历史点击次数
export class dexieHistoryTag {
  // 详情查看table.put介绍  根据主键是否存在 判断为新增或更新
  save(tag: Tag, key:any) {
    return db.transaction('rw', db.historyTag, () => {
      return db.historyTag.put(tag, key)
    });
  }
  async selectById(id:any){
    return await db.historyTag.get({id})
  }
  async selectAll(){
    return await db.historyTag.filter(() => true).toArray()
  }
}

export class setingConfig {
   // 详情查看table.put介绍  根据主键是否存在 判断为新增或更新
   save(seting: Seting, key:any) {
     debugger
    return db.transaction('rw', db.seting, () => {
      return db.seting.put(seting, key)
    });
  }
  async select(){
    return await db.seting.filter(() => true).toArray()
  }
}
